<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | nexFlow Plugin v3.1.1 for the nexPro Portal Server                        |
// | January 2010                                                              |
// | Developed by Nextide Inc. as part of the nexPro suite - www.nextide.ca    |
// +---------------------------------------------------------------------------+
// | ajaxupdate_tvars.php                                                      |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2007-2010 by the following authors:                         |
// | Blaine Lang            - Blaine DOT Lang AT nextide DOT ca                |
// | Randy Kolenko          - Randy DOT Kolenko AT nextide DOT ca              |
// | Eric de la Chevrotiere - Eric DOT delaChevrotiere AT nextide DOT ca       |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+
//

require_once("../../../lib-common.php"); // Path to your lib-common.php

if (!SEC_hasRights('nexflow.edit')) {
    print ('No access rights');
    exit();
}

$mode = COM_applyFilter($_GET['mode']);
$rec = COM_applyFilter($_GET['rec'],true);
$op = COM_applyFilter($_GET['op']);
$tvar = COM_applyFilter($_GET['variable']);
if (!get_magic_quotes_gpc()) {
    $tvalue = addslashes($_GET['value']);
} else {
    $tvalue = $_GET['value'];
}


// Return a HTML formatted table of the template variables that will be replaced in the users browser
function generateTemplateVariableHTML($rec,$cntr) {
    global $_TABLES,$_CONF;

    $p = new Template($_CONF['path_layout'] . 'nexflow/admin');
    $p->set_file ('variables','template_variables.thtml');
    $p->set_file ('variable_rec','template_variable_record.thtml');

    $p->set_var('template_id',$rec);
    $p->set_var('cntr',$cntr);

    $sql = "SELECT * FROM {$_TABLES['nf_templatevariables']} WHERE nf_templateID='{$rec}' ORDER BY id";
    $query = DB_Query($sql);
    $numrows = DB_numrows($query);
    if ($numrows > 0) {
        $j=1;
        $p->set_var('show_vars','');
        $p->set_var('vdivid','');

        while ( $A = DB_fetchArray($query) ) {
            $edit_link = "[&nbsp;<a href=\"#\" onClick='ajaxUpdateTemplateVar(\"edit\",{$rec},{$cntr},{$j});'\">Edit</a>&nbsp;]";
            $del_link = "[&nbsp;<a href=\"#\" onClick='ajaxUpdateTemplateVar(\"delete\",{$rec},{$cntr},{$j});'\">Delete</a>&nbsp;]";
            $p->set_var('variable_name',$A['variableName']);
            $p->set_var('variable_value',$A['variableValue']);
            $p->set_var('var_id',$j);
            $p->set_var('edit_link',$edit_link);
            $p->set_var('delete_link',$del_link);
            if ($j == 1) {
                $p->parse('template_variable_records','variable_rec');
            } else {
                $p->parse('template_variable_records','variable_rec',true);
            }
            $j++;
        }
    } else {
        $p->set_var('show_vars','none');
        $p->set_var('vdivid',"vars{$cntr}");
        $p->set_var('template_variable_records','');
    }
    $p->parse ('output', 'variables');
    $html = $p->finish ($p->get_var('output'));
    $html = htmlentities($html);

    return $html;
}


// Main Control Section Begins

// Need to retrieve the value of counter id that was uses in the displayed html generated by templates.php
$tquery = DB_query("SELECT id,templateName FROM {$_TABLES["nf_template"]} ORDER BY id");
$cntr = 0;
while (list ($templateId, $templateName) = DB_fetchArray($tquery)) {
    if ($rec == $templateId) {
        break;
    }
    $cntr++;
}

if ($op == 'add') {

    DB_query("INSERT into {$_TABLES['nf_templatevariables']} (nf_templateID,variableName,variableValue) values('{$rec}','{$tvar}','{$tvalue}')");
    $retval =  generateTemplateVariableHTML($rec,$cntr);

} elseif ($op == 'update') {

    DB_query("UPDATE {$_TABLES['nf_templatevariables']} set variableValue='{$tvalue}' WHERE nf_templateID='$rec' AND variableName='$tvar'");
    $retval =  generateTemplateVariableHTML($rec,$cntr);

} elseif ($op == 'delete') {

    $variable_id = DB_getItem($_TABLES['nf_templatevariables'], 'id', "nf_templateID='$rec' AND variableName='$tvar'");
    if ($CONF_NF['debug']) {
        COM_errorLog("Delete variable: rec:$rec, tvar:$tvar -> $variable_id");
    }
    DB_query("DELETE FROM {$_TABLES['nf_templateassignment']} WHERE nf_processVariable='$variable_id'");
    DB_query("DELETE FROM {$_TABLES['nf_templatevariables']} WHERE nf_templateID='$rec' AND variableName='$tvar'");
    if ($CONF_NF['debug']) {
        COM_errorLog("DELETE FROM {$_TABLES['nf_templatevariables']} WHERE nf_templateID='$rec' AND variableName='$tvar'");
    }
    $retval =  generateTemplateVariableHTML($rec,$cntr);

} elseif ($op == 'updateTemplateName') {

    if ($CONF_NF['debug']) {
        COM_errorLog("UPDATE {$_TABLES['nf_template']} set templateName='{$tvalue}' WHERE id='$rec'");
    }
    DB_query("UPDATE {$_TABLES['nf_template']} set templateName='{$tvalue}' WHERE id='$rec'");
    $retval = $tvalue;

} elseif ($op == 'useProject') {

    DB_query("UPDATE {$_TABLES['nf_template']} set useProject=NOT(useProject) WHERE id='$rec'");

} elseif ($op == 'updateApplicationGroup') {
    $tvalue=COM_applyFilter($tvalue,true);
    DB_query("UPDATE {$_TABLES['nf_template']} set AppGroup='{$tvalue}' WHERE id='$rec'");

}


else {
    $retval =  generateTemplateVariableHTML($rec,$cntr);
}


header("Cache-Control: no-store, no-cache, must-revalidate");
header("content-type: text/xml");
$XML = "<result>";
$XML .= "<record>$rec</record>";
$XML .= "<operation>$op</operation>";
$XML .= "<value1>$retval</value1>";
$XML .= "<value2>$cntr</value2>";
$XML .= "</result>";
print $XML;


?>